package cc.orange.mainView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.bumptech.glide.Glide;
import com.tendcloud.tenddata.TalkingDataSDK;
import com.xcheng.retrofit.Call;
import com.xcheng.retrofit.DefaultCallback;
import com.xcheng.retrofit.HttpError;
import com.xcheng.retrofit.RetrofitFactory;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cc.orange.R;
import cc.orange.base.BaseActivity;
import cc.orange.databinding.ActivityMyGradeBinding;
import cc.orange.entity.GradeInfo;
import cc.orange.entity.HightEntity;
import cc.orange.entity.IsLoginsEntity;
import cc.orange.http.ApiService;
import cc.orange.utils.StatusBarUtilView;
import cc.orange.utils.ZXToastUtil;

public class MyGradeActivity extends BaseActivity {

    private ActivityMyGradeBinding binding;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_my_grade);

        binding.infosetImg3.setOnClickListener(view -> finish());
        binding.tvGradeToList.setOnClickListener(view -> {
            startActivity(new Intent(this,GradeListActivity.class));
        });
        getMyGrade();

        EventBus.getDefault().register(this);
    }

    private void getMyGrade() {
        RetrofitFactory.create(ApiService.class)
                .myLevel(getTokens())
                .enqueue(new DefaultCallback<GradeInfo>() {
                    @Override
                    public void onStart(Call<GradeInfo> call) {
                        showLoading_base(MyGradeActivity.this);
                    }

                    @Override
                    public void onError(Call<GradeInfo> call, HttpError error) {
                        cancelLoading();
                    }

                    @Override
                    public void onSuccess(Call<GradeInfo> call, GradeInfo gradeInfo) {
                        cancelLoading();
                        if (gradeInfo.getCode() == 0) {
                            GradeInfo.DataDTO data = gradeInfo.getData();
                            Glide.with(MyGradeActivity.this).load(data.getPhoto()).into(binding.civHead);

                            binding.tvGradeName.setText(data.getNumber());
                            binding.tvExperience.setText(data.getLevelNum() + "");
                            binding.pbGrade.setProgress(data.getLevelNum());
                        } else if (gradeInfo.getCode() == 401) {
                            EventBus.getDefault().post(new IsLoginsEntity(false));
                        } else {
                            ZXToastUtil.showToast(gradeInfo.getMsg());
                        }
                    }
                });
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        TalkingDataSDK.onPageBegin(this, getLocalClassName());
    }

    @Override
    protected void onPause() {
        super.onPause();
        TalkingDataSDK.onPageEnd(this, getLocalClassName());
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        StatusBarUtilView.transparencyBar(MyGradeActivity.this);
        StatusBarUtilView.setLightStatusBar(MyGradeActivity.this, true, true);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void getHight(HightEntity entity) {//改变布局
        if(entity.getHight() > 0){
            RelativeLayout.LayoutParams pp = new RelativeLayout.
                    LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    entity.getHight());
            binding.homeTitle.setLayoutParams(pp);
        }
    }

}
